/**
* @ProjectName: ShopSytem
* @Title: UserRegister.java 
* @Package com.element5th.shop.servlet.user 
* @Description: 用户注册
* @author 第五元素小组   
* @date 2012-10-26 16:57:30 
* @version V1.0   
*/
package com.element5th.shop.servlet.user;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.Factory;
import org.apache.log4j.chainsaw.Main;

import com.element5th.comm.DBHelper;
import com.element5th.shop.comm.DaoFactory;
import com.element5th.shop.comm.SystemException;
import com.element5th.shop.dto.BaseDto;
import com.element5th.shop.entity.User;
import com.element5th.shop.entity.Userbase;

/**
 * Servlet implementation class UserRegister
 */
@WebServlet("/user_register")
public class UserRegister extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#service(HttpServletrequestuest requestuest, HttpServletresponseonse responseonse)
	 */
	protected void service(HttpServletRequest request, ServletResponse response) 
			throws ServletException, IOException {
		
	}


	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			 {
	
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
			
			
			BaseDto userDto=new BaseDto();
			User user=userDto.getUser();
			
			
			BaseDto userBaseDto=new BaseDto();
			Userbase userBase=userBaseDto.getUserBase();
			userBase.setUserbaseName(request.getParameter("name"));
			
			
			
			
			Connection conn = DBHelper.getConnection();
			PreparedStatement ps = null;
			ResultSet rs=null;
			
			String sql="select seq_userbase.nextval from dual";
			int baseId=0;
			try {
				ps=conn.prepareStatement(sql);
				rs=ps.executeQuery();
				rs.next();
				baseId=rs.getInt(1);
				
			} catch (SQLException e) {
			     System.out.println(e.getMessage());
			}
			System.out.println(baseId);
			userBase.setUserbaseId(baseId);
			userBaseDto.setUserBase(userBase);
			DaoFactory.getUserDao().userBaseRegister(userBaseDto);
			
			user.setUserbaseId(baseId);
			user.setUserName(request.getParameter("name"));
			user.setUserNickname(request.getParameter("nick"));
			user.setUserPwd(request.getParameter("password"));
			user.setUserEmail(request.getParameter("email"));
			user.setUserPhone(request.getParameter("telphone"));
					
			System.out.println(user.getUserName());
			
			userDto.setUser(user);
			DaoFactory.getUserDao().userRegister(userDto);
		    
	}
	
	

}
